package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.BaseSaleAttr;
import com.atguigu.gmall.model.product.SpuImage;
import com.atguigu.gmall.model.product.SpuInfo;
import com.atguigu.gmall.model.product.SpuSaleAttr;
import com.atguigu.gmall.product.service.ManageService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/admin/product")
public class SpuManageController {

    @Autowired
    private ManageService manageService;

    //  /admin/product/{page}/{limit}
    //  http://api.gmall.com/admin/product/1/10?category3Id=61
    //  ?category3Id=61 这个参数是普通传递！ 如何获取这个条件！ javaWeb
    @GetMapping("/{page}/{limit}")
    public Result getSpuInfoList(@PathVariable Long page,
                                 @PathVariable Long limit,
                                 SpuInfo spuInfo){

        //  创建一个Page 对象
        Page<SpuInfo> spuInfoPage = new Page<>(page,limit);
        //  调用服务层方法查询.
        IPage iPage = this.manageService.getSpuInfoList(spuInfoPage,spuInfo.getCategory3Id());
        //  返回数据.
        return Result.ok(iPage);
    }

    //  /admin/product/baseSaleAttrList
    @GetMapping("baseSaleAttrList")
    public Result getBaseSaleAttrList(){
        //  select * from base_sale_attr;
        List<BaseSaleAttr> baseSaleAttrList = this.manageService.getBaseSaleAttrList();
        //  返回销售属性列表
        return Result.ok(baseSaleAttrList);
    }

    //  保存数据：
    //  /admin/product/saveSpuInfo
    //  Json -> JavaObject;
    @PostMapping("/saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo){
        //  调用服务层方法.
        this.manageService.saveSpuInfo(spuInfo);
        return Result.ok();
    }

    //  根据spuId 查询图片列表
    @GetMapping("/spuImageList/{spuId}")
    public Result getSpuImageList(@PathVariable Long spuId){
        //  获取数据.
        List<SpuImage> spuImageList = this.manageService.getSpuImageList(spuId);
        return Result.ok(spuImageList);
    }

    //  回显销售属性+销售属性值：
    @GetMapping("/spuSaleAttrList/{spuId}")
    public Result getSpuSaleAttrList(@PathVariable Long spuId){
        //  获取数据.
        List<SpuSaleAttr> spuSaleAttrList = this.manageService.getSpuSaleAttrList(spuId);

        return Result.ok(spuSaleAttrList);
    }

}
